/*
*************************************************************************
*西瑞克斯研发管理系统
*版 本: 1.0   
*日 期：02/11/2012    
*作 者： 王浩 QQ：873987060   
*************************************************************************   
*/ 
package com.manager.workflow.hander;

import java.util.ArrayList;
import java.util.List;

import org.fireflow.engine.EngineException;
import org.fireflow.engine.IProcessInstance;
import org.fireflow.engine.IWorkItem;
import org.fireflow.engine.IWorkflowSession;
import org.fireflow.engine.impl.TaskInstance;
import org.fireflow.engine.taskinstance.DynamicAssignmentHandler;
import org.fireflow.engine.taskinstance.IAssignable;
import org.fireflow.engine.taskinstance.IAssignmentHandler;
import org.fireflow.kernel.KernelException;

import com.application.usertable.entity.Usertable;
import com.manager.commons.CurrentUserUtils;
/**
 * 该AssignmentHandler将TaskInstance分配给当前用户，并且立即结束新创建的工作项。   
 * 适用于填写表单。
 * 
 * @author 王浩 QQ：873987060   
 *
 */
public class ZhiPaiCurrentUserAssignmentHandler implements IAssignmentHandler {

	@Override
	public void assign(IAssignable able, String role) throws EngineException,KernelException {
		TaskInstance taskInst = (TaskInstance)able;
		IWorkflowSession workSession = taskInst.getCurrentWorkflowSession();
		//取得当前系统用户
		Usertable user = CurrentUserUtils.getCurrentUser();
		//为当前用户创建WorkItem
		IWorkItem wi = able.asignToActor(user.getId());
		
		//签收和完成工作项目使得流程往下走
		wi.claim();
		
		IProcessInstance processInstance = ((TaskInstance)wi.getTaskInstance()).getAliveProcessInstance();
		String bzpr = (String) processInstance.getProcessInstanceVariable("projectOrg"); //被指派人
		System.out.println("任务指派给：" + bzpr);
		List actorIdsList = new ArrayList();
		actorIdsList.add(bzpr);
		DynamicAssignmentHandler dynamic = new DynamicAssignmentHandler();
		dynamic.setActorIdsList(actorIdsList);
		workSession.setAttribute("dynamicHandler", dynamic);
		
		
		wi.complete();
	}

}
